package com.mingmen.canting.caigourenyuan;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.PeiSongDetailModel;

import java.util.List;


public class QueRenZiCaiAdapter extends RecyclerView.Adapter<QueRenZiCaiAdapter.ViewHolder> {
    List<PeiSongDetailModel.DataBean.DetailsBean> myLiveList;
    Context mContext;
    private OnItemClickListener mOnItemClickListener;


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<PeiSongDetailModel.DataBean.DetailsBean> myLiveList);
    }


    public QueRenZiCaiAdapter(Context context, List<PeiSongDetailModel.DataBean.DetailsBean> list) {
        this.mContext = context;
        this.myLiveList = list;
    }

    public void setDataList(List<PeiSongDetailModel.DataBean.DetailsBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_querenzicai, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        PeiSongDetailModel.DataBean.DetailsBean detailsBean = myLiveList.get(position);
        holder.checkbox.setChecked(myLiveList.get(position).isSelect());
        Glide.with(mContext).load(AppConfig.IMGURL + detailsBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);

        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                myLive.setChecked(!myLive.isChecked());
                myLiveList.get(position).setSelect(!myLiveList.get(position).isSelect());
                holder.checkbox.setChecked(myLiveList.get(position).isSelect());
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });

        holder.mTvTitle.setText(myLiveList.get(position).getCommodityName());
        holder.amount.setText("计划数量: " + myLiveList.get(position).getAmount() + "");
        holder.specs.setText("规格: " + myLiveList.get(position).getAmount() + "");
//        holder.confirmAmount.setText("实发数量: " + myLiveList.get(position).getConfirmPrice() + "");
        holder.arriveAmount.setText(myLiveList.get(position).getArriveAmount() + "");
        holder.arrivePrice.setText(myLiveList.get(position).getArrivePrice() + "");
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });
        holder.lyaout_shishou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClickListener(view, position, myLiveList);
            }
        });
        holder.layout_shijia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {


        TextView mTvTitle;

        ImageView ivAvatar;


        CheckBox checkbox;
        LinearLayout content;


        LinearLayout layout_guige;


        TextView arriveAmount;
        TextView amount;
        TextView arrivePrice;

        TextView specs;
        LinearLayout lyaout_shishou;

        LinearLayout layout_shijia;

        public ViewHolder(View itemView) {
            super(itemView);

            mTvTitle = (TextView) itemView.findViewById(R.id.tvCity);
            ivAvatar = (ImageView) itemView.findViewById(R.id.ivAvatar);


            checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
            content = (LinearLayout) itemView.findViewById(R.id.content);


            layout_guige = itemView.findViewById(R.id.layout_guige);

            arriveAmount = itemView.findViewById(R.id.arriveAmount);
            amount = itemView.findViewById(R.id.amount);
            arrivePrice = itemView.findViewById(R.id.arrivePrice);
            specs = itemView.findViewById(R.id.specs);
            lyaout_shishou = itemView.findViewById(R.id.lyaout_shishou);
            layout_shijia = itemView.findViewById(R.id.layout_shijia);
        }
    }


}
